﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MuaDeal.DTO;

namespace MuaDeal
{
    public partial class ThemSanPham : Form
    {
        public ThemSanPham()
        {
            InitializeComponent();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }
        DataTable dtlinhvuc = new DataTable();
        DataTable dtcongty = new DataTable();
        private void ThemSanPham_Load(object sender, EventArgs e)
        {
            string procc = "sp_LayDanhSachCongTy";
            string proclv = "sp_LayDSLinhVuc";
            try
            {
                Datahelper helper=new Datahelper();
                helper.Open();
                dtcongty = helper.ExecuteQueryStore(procc);
                dtlinhvuc = helper.ExecuteQueryStore(proclv);
                helper.Close();
                cmbcongty.DataSource = dtcongty;
                cmbcongty.DisplayMember = dtcongty.Columns[1].ToString();
                cmbcongty.ValueMember = dtcongty.Columns[0].ToString();

                cmblinhvuc.DataSource = dtlinhvuc;
                cmblinhvuc.DisplayMember = dtlinhvuc.Columns[1].ToString();
                cmblinhvuc.ValueMember = dtlinhvuc.Columns[0].ToString();
            }catch(SqlException ex){}
        }

        private void btnthemsanpham_Click(object sender, EventArgs e)
        {
            if (txtgiaban.Text != "" && txtgiadagiam.Text != "" && txtsoluong.Text != "" && txttensp.Text != "")
            {
                #region LAY SAN PHAM TU GIAO DIEN
                sanPhamDTO sp=new sanPhamDTO();
                sp.TenSanPham = txttensp.Text;
                float temp = 0;
                bool kq=float.TryParse(txtgiaban.Text,out temp);
                sp.DonGia = temp;
                kq = float.TryParse(txtgiadagiam.Text, out temp);
                sp.GiaDaGiam = temp;

                sp.MaCongTy = "ct002";
                sp.MaCongTy = cmbcongty.SelectedValue.ToString();
                
                int temp1 = 1;
                kq = int.TryParse(cmblinhvuc.SelectedValue.ToString(), out temp1);
                sp.MaLinhVuc = temp1;
                kq = int.TryParse(txtsoluong.Text, out temp1);
                sp.SoLuongTon = temp1;
                sp.ThoiGianBatDau = dateTimePicker1.Value;//.ToString("yyyy-MM-dd hh:mm:ss");
                sp.ThoiGianKetThuc = dateTimePicker2.Value;//.ToString("yyyy-MM-dd hh:mm:ss");
                sp.TinhTrangSanPham = 1;
                sp.ChiTietSanPham = "chưa cập nhật chi tiết.";

                string ngaybatdau = sp.ThoiGianBatDau.ToString("yyyy-MM-dd hh:mm:ss");
                string ngayketthuc = sp.ThoiGianKetThuc.ToString("yyyy-MM-dd hh:mm:ss");
               
                #endregion

                int ketqua = 0;
                #region THEM VAO CO SO DU LIEU
                try
                {
                    Datahelper helper = new Datahelper();
                    helper.Open();
                    SqlCommand comman = new SqlCommand();
                    //SqlTransaction sqltran=helper.Connection.BeginTransaction();
                    //comman.Transaction= sqltran;
                    String chuoi = "sp_ThemSP";
                    if (rdbtnsai.Checked == true)
                    {
                        chuoi = "sp_ThemSP_NoDelay";
                    }
                    SqlParameter bien1 = comman.Parameters.Add("@malinhvuc", SqlDbType.Int);
                    bien1.Direction = ParameterDirection.Input;
                    bien1.Value = sp.MaLinhVuc;


                    SqlParameter bien10 = comman.Parameters.Add("@tensanpham", SqlDbType.NVarChar, 100);
                    bien10.Direction = ParameterDirection.Input;
                    bien10.Value = sp.TenSanPham;


                    SqlParameter bien3 = comman.Parameters.Add("@dongia", SqlDbType.Float);
                    bien3.Direction = ParameterDirection.Input;
                    bien3.Value = sp.DonGia;

                    SqlParameter bien4 = comman.Parameters.Add("@giadagiam", SqlDbType.Float);
                    bien4.Direction = ParameterDirection.Input;
                    bien4.Value = sp.GiaDaGiam;

                    SqlParameter bien5 = comman.Parameters.Add("@tgbatdau", SqlDbType.Char,17);
                    bien5.Direction = ParameterDirection.Input;
                    bien5.Value = "2011-1-1";//ngaybatdau;

                    SqlParameter bien9 = comman.Parameters.Add("@tgketthuc", SqlDbType.Char,17);
                    bien9.Direction = ParameterDirection.Input;
                    bien9.Value = "2011-1-3";//ngayketthuc;

                    SqlParameter bien6 = comman.Parameters.Add("@soluongton", SqlDbType.Int);
                    bien6.Direction = ParameterDirection.Input;
                    bien6.Value = sp.SoLuongTon;

                    SqlParameter bien7 = comman.Parameters.Add("@matinhtrangsp", SqlDbType.Int);
                    bien7.Direction = ParameterDirection.Input;
                    bien7.Value = sp.TinhTrangSanPham;

                    SqlParameter bien8 = comman.Parameters.Add("@chitiet", SqlDbType.NVarChar, 1000);
                    bien8.Direction = ParameterDirection.Input;
                    bien8.Value = sp.ChiTietSanPham;

                    SqlParameter bien2 = comman.Parameters.Add("@macongty", SqlDbType.Char, 10);
                    bien2.Direction = ParameterDirection.Input;
                    bien2.Value = sp.MaCongTy;

                    comman.CommandText = chuoi;
                    comman.CommandType = CommandType.StoredProcedure;
                    comman.Connection = helper.Connection;
                    ketqua = comman.ExecuteNonQuery();
                    helper.Close();
                }
                catch (SqlException ex1) { MessageBox.Show(ex1.Message); }
                //Nếu cập nhật thành công thì thông báo message box và load lại listview
                if (ketqua == 1)
                {
                    MessageBox.Show("Đã thêm sản phẩm thành công");                  
                }
                else
                {
                    MessageBox.Show("Thêm sản phẩm không thành công");
                }
                #endregion

            }
            else
            {
                MessageBox.Show("chưa nhập đầy đủ thông tin.");
                return;
            }
        }

        private void btnxemdanhsachsp_Click(object sender, EventArgs e)
        {
            XemDSSanPham frm = new XemDSSanPham();
            frm.ShowDialog();
        }
    }
}